//
//  FSAttentionController.m
//  fanshu
//
//  Created by 许海峰 on 16/3/27.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSAttentionController.h"
#import "FSTitleBtn.h"
#import "FSFindInterestingBtn.h"
#import "FSAddAttentionController.h"
#import "FSSearchController.h"
#import "FSNoticSettingViewController.h"
#import "FSAttentionItem.h"
#import "FSSpecialTopicHomePageViewController.h"

@interface FSAttentionController()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *attentionItemArray;
@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,weak) UIView *commendBackgroundView;

@property (nonatomic,weak) UIView *coverView;

@end
@implementation FSAttentionController
//懒加载
-(NSMutableArray *)attentionItemArray
{
    if (_attentionItemArray == nil) {
        _attentionItemArray = [NSMutableArray array];
    }
    return _attentionItemArray;
}


-(void)viewDidLoad
{
    //设置导航条
    [self addAllNavigationItem];
    self.automaticallyAdjustsScrollViewInsets = NO;
    //是否已有关注的人或文章
    BOOL NoAttention = YES;
    if (NoAttention) { //还没有登录
        [self addCommendAttentionBtn];
    } else { //已经登录
        [self addFriendBehaviourAndAllAttentionView];
        //加载titleView
        [self addNavigationItemTitleView];
    }
}

#pragma mark 登录前界面
//添加未登录界面的推荐按钮
-(void)addCommendAttentionBtn
{
    UIView *commendBackgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:commendBackgroundView];
    self.commendBackgroundView = commendBackgroundView;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.bounds = CGRectMake(0, 0, 324, 141);
    imageView.center = self.view.center;
    [commendBackgroundView addSubview:imageView];
    imageView.image = [UIImage imageNamed:@"img_guide_subscribe"];
    //添加感兴趣按钮
    
    FSFindInterestingBtn *findInterestingBtn = [FSFindInterestingBtn buttonWithType:UIButtonTypeCustom];
    findInterestingBtn.center = CGPointMake(self.view.centerX, self.view.height * 0.7);
    [commendBackgroundView addSubview:findInterestingBtn];
    [findInterestingBtn addTarget:self action:@selector(findInterestingBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    //添加一个测试按钮
    UIButton *testBtn = [[UIButton alloc] init];
    [testBtn setTitle:@"测试按钮,点击切换" forState:UIControlStateNormal];
    [testBtn addTarget:self action:@selector(addFriendBehaviourAndAllAttentionView) forControlEvents:UIControlEventTouchUpInside];
    [testBtn sizeToFit];
    [commendBackgroundView addSubview:testBtn];
    testBtn.center = CGPointMake(self.view.centerX, self.view.height * 0.2);
}
//点击推荐按钮
-(void)findInterestingBtnClick
{
    self.tabBarController.selectedIndex = 0;
}

#pragma mark 登录后界面
//添加朋友动态和关注的人界面(登录后界面)
-(void)addFriendBehaviourAndAllAttentionView
{
    //加载titleView
    [self addNavigationItemTitleView];
    //模拟联网数据
    [self loadWebData];
    //切换界面
    [self.commendBackgroundView removeFromSuperview];

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.rowHeight = 60;
    self.tableView = tableView;
    
    //调整(iOS7以上)表格分隔线
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        tableView.separatorInset = UIEdgeInsetsZero;
    }
    //调整(iOS8以上)表格分隔线
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        tableView.layoutMargins = UIEdgeInsetsZero;
    }
    [self setupTableViewHeaderFooter];
    
   

}
-(void)setupTableViewHeaderFooter
{
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 35)];
//    header.backgroundColor = [UIColor greenColor];
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(5, 5, self.view.width - 10, 25);
    
    [searchBtn setBackgroundImage:[UIImage resizableImageWithImageName:@"button_write_md_key"] forState:UIControlStateNormal];
    [searchBtn setImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
    [searchBtn setTitle:@"搜索关注源" forState:UIControlStateNormal];
    [searchBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [searchBtn addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:searchBtn];
    
    UIView *lineH = [[UIView alloc] initWithFrame:CGRectMake(0, 34.5, ScreenWidth, 0.5)];
    lineH.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.7];
    [header addSubview:lineH];

    self.tableView.tableHeaderView = header;
    
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 40)];
    
    UILabel *endLabel = [[UILabel alloc] init];
    endLabel.text = @"-- end --";
    [endLabel sizeToFit];
    endLabel.centerX = self.tableView.width * 0.5;
    
    endLabel.font = [UIFont systemFontOfSize:12];
    endLabel.textColor = [UIColor grayColor];
    [footer addSubview:endLabel];
    
    UIView *lineF = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.5)];
    lineF.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.7];
    [footer addSubview:lineF];
    self.tableView.tableFooterView = footer;
}

- (void)search{
    //model  搜索控制器
    FSSearchController *Vc = [[FSSearchController alloc] init];
    Vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:Vc animated:YES completion:nil];
}

-(void)loadWebData
{
    self.attentionItemArray = nil;
    //假数据
    FSAttentionItem *item0 = [[FSAttentionItem alloc] init];
    item0.title = @"简友动态";
    item0.icon = @"icon_publish_share_image";
    item0.subtitle = nil;
    [self.attentionItemArray addObject:item0];
    
    FSAttentionItem *item1 = [[FSAttentionItem alloc] init];
    item1.title = @"一键查看全部关注更新";
    item1.icon = @"icon_publish_share_image";
    item1.subtitle = nil;
    [self.attentionItemArray addObject:item1];
    
   //判断关注类型
    UIButton *titleBtn = (UIButton *)self.navigationItem.titleView;
    NSString *typeStr = titleBtn.titleLabel.text;
    
    FSAttentionItem *item2 = [[FSAttentionItem alloc] init];
    item2.title = [NSString stringWithFormat:@"动态数据--%@",typeStr];
    item2.icon = @"icon_publish_share_image";
    item2.subtitle = @"动态数据动态数据动态数据动态数据动态数据";
    
    [self.attentionItemArray addObject:item2];
    [self.attentionItemArray addObject:item2];
    [self.attentionItemArray addObject:item2];
    [self.attentionItemArray addObject:item2];
    [self.attentionItemArray addObject:item2];
    
    [self.tableView reloadData];
}

#pragma mark 添加导航栏项目
//添加导航栏项目
-(void)addAllNavigationItem
{
    //添加左按钮
    UIImage *leftImage = [UIImage imageNamed:@"icon_side_add"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(addBtnClick)];
    //添加右按钮
    UIImage *rightImage = [UIImage imageNamed:@"icon_notification_settings"];
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStylePlain target:self action:@selector(noticSettingBtnClick)];
    
   }
//点击左边添加按钮
-(void)addBtnClick
{
    FSAddAttentionController *addAttentionVC = [[FSAddAttentionController alloc] init];
    
    [self.navigationController pushViewController:addAttentionVC animated:YES];
    
}
//点击右边设置按钮
-(void)noticSettingBtnClick
{
    FSNoticSettingViewController *noticVC = [[FSNoticSettingViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [self.navigationController pushViewController:noticVC animated:YES];
//    NSLog(@"%s",__func__);
}
-(void)addNavigationItemTitleView {
    //设置标题按钮
    FSTitleBtn *btn = [FSTitleBtn buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"全部关注" forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"icon_following_arrow_open"] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn sizeToFit];
    
    self.navigationItem.titleView = btn;
    [btn addTarget:self action:@selector(attentionToAllBtnClick) forControlEvents:UIControlEventTouchUpInside];
    

}
//点击中间关注按钮
-(void)attentionToAllBtnClick
{
//    NSLog(@"%s",__func__);
    if (_coverView == nil) {
        UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:backgroundView];
        //    backgroundView.backgroundColor = [UIColor redColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeCoverView)];
        [backgroundView addGestureRecognizer:tap];
        self.coverView = backgroundView;
        
        UIView * blackView = [[UIView alloc] init];
        blackView.backgroundColor = [UIColor blackColor];
        CGFloat width = 150;
        CGFloat height = 200;
        blackView.frame = CGRectMake((ScreenWidth - width) * 0.5, 60, width, height);
        [backgroundView addSubview:blackView];
        
        NSArray *typeArray = @[@"全部关注",@"只看专题",@"只看文集",@"只看用户",@"只看关注更新"];
        for (int i = 0; i < 5; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
            [btn setTitle:typeArray[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
            CGFloat btnW = blackView.width;
            CGFloat btnH = 40;
            btn.frame = CGRectMake(0, btnH * i, btnW, btnH);
            [blackView addSubview:btn];
            btn.tag = i;
            [btn addTarget:self action:@selector(attentionTypeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    } else {
        [_coverView removeFromSuperview];
    }
}
-(void)attentionTypeBtnClick:(UIButton *)btn {
    NSArray *typeArray = @[@"全部关注",@"只看专题",@"只看文集",@"只看用户",@"只看关注更新"];
//    NSLog(@"%@",typeArray[btn.tag]);
    UIButton *titleBtn = (UIButton *)self.navigationItem.titleView;
    [titleBtn setTitle:typeArray[btn.tag] forState:UIControlStateNormal];
    [self.coverView removeFromSuperview];
    
    //刷新数据
    [self loadWebData];
    
}
-(void)removeCoverView {
    [self.coverView removeFromSuperview];
}

#pragma mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.attentionItemArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"attentionCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    FSAttentionItem *item = self.attentionItemArray[indexPath.row];
    cell.textLabel.text = item.title;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    cell.imageView.image = [UIImage imageNamed:item.icon];
    cell.detailTextLabel.text = item.subtitle;
    cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
    cell.detailTextLabel.textColor = [UIColor grayColor];
//    cell.detailTextLabel.y += 15;
    
    //iOS8以后cell分隔线边距调整
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
//    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
//        cell.preservesSuperviewLayoutMargins = NO;
//        
//    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row > 1) {
        FSAttentionItem *item = self.attentionItemArray[indexPath.row];
        FSSpecialTopicHomePageViewController *STHomeVC = [[FSSpecialTopicHomePageViewController alloc] init];
        [self.navigationController pushViewController:STHomeVC animated:YES];
    }
}
@end
